#include "Timer.h"
#include "ui_Timer.h"

Timer::Timer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Timer)
{
    ui->setupUi(this);
    timer1=new QTimer();
    QObject:: connect( timer1, SIGNAL(timeout()), SLOT(timeDone1()) );
     timer2=new QTimer();
    QObject:: connect( timer2, SIGNAL(timeout()), SLOT(timeDone2()) );
     timer2->setSingleShot(true);
     overtime_timer=new QTimer();
     QObject:: connect( overtime_timer, SIGNAL(timeout()), SLOT(overtime_timeDone()) );
     overtime_timer->setSingleShot(true);
}

Timer::~Timer()
{
    delete ui;
}

void Timer::countdown(uint t)
{
    countup=0;
    count=t;
    timer1->start(1000);
    timer2->start(1000*t);
    show();
}

void Timer::counter_start(uint sec)
{
     clear();
     countup=1;
     count=0;
     timer1->start(sec);
     show();
}

void Timer::open_overtimer(uint sec)
{
    if(!overtime_timer->isActive())  overtime_timer->start(sec *1000);
}

void Timer::close_overtimer()
{
    if(overtime_timer->isActive())  overtime_timer->stop();
}

void Timer::timeDone1()
{
    countup?ui->lcdNumber->display(++count):ui->lcdNumber->display(--count);
}


void Timer::timeDone2()
{
    timer1->stop();
    hide();
}

void Timer::overtime_timeDone()
{
     emit overtime_signal();
}

 void Timer::clear()
{
    if(timer1->isActive())  timer1->stop();
    if(timer2->isActive()) timer2->stop();
    ui->lcdNumber->display(0);
    hide();
}

 void Timer::stop()
{
     if(timer1->isActive())  timer1->stop();
     if(timer2->isActive()) timer2->stop();
 }
